<?php
/**
 * @author : Quyenvv
 */
class  Model_Danhmuc extends Zend_Db_Table
{
	protected  $_db ;
	protected  $_dm = "dm_loai_thu";
	protected  $_tenndm = "ten_nhom_dm";
	
	
	public function __construct()
	{
	  $this->_db =  Zend_Registry::get('connectDb');
	  $this->_date =  Zend_Date::now(); // date('d-m-Y H:i:s');
	}
	
	public  function getallchung()
	{
		$sql = $this->_db->select()
						 ->from($this->_dm)
						 ->order('sap_xep ASC');
		$query = $this->_db->fetchAll($sql);
	return $query;						 
	}
	
  	public function xoadanhmuc($iddm)
    {
    	$where = 'id ='.$iddm;
    	$xoa = $this->_db->delete($this->_dm,$where);
    }
	
	public function deletealldanhmuc($mangid)
	{
	    $arrayid = explode(',', $mangid);
    	for ($i =0;$i< count($arrayid);$i++){
    		$id = $arrayid[$i];
    		//sql xóa
    		$where = 'id = '.$id;
    		$xoa = $this->_db->delete($this->_dm,$where);
    		
    	}
	}

	public function getallnhomdmtheoiddn($data)
		{
			$sql = $this->_db->select()
							 ->from($this->_dm)
							 ->where('uid = ?',$data['iddn'])
							 ->order('id DESC');
			$query = $this->_db->fetchAll($sql);
		 return  $query;
		}
	
	public  function adddanhmuc($data,$uid)
	{
		
		$arr = array(
			'uid'			=> $uid,
		    'ten_loai_thu'	=> $data['tendanhmuc'],
		    'sap_xep'	    => $data['sapxep'],
			'ngay_tao'	    => $this->_date,
			'ngay_sua'	    => $this->_date,
		
		);
		$query = $this->_db->insert($this->_dm, $arr);
	return $query;
	}
	
	/**
	 * Quyen VV
	 * Enter description : update danh mục
	 */
	
	public function getupdatedanhmuc($data)
	{
		$date = date('m-d-Y H:i:s');
		$arr = array(
			'su_dung'   => $data['thamso'],
			'ngay_sua'	=>$date
		);
	  $where = 'id = '.$data['id'];	
	  $query = $this->_db->update($this->_dm, $arr,$where)	;
	 return $query; 
	}
	
	public function updatedanhmuc($data)
	{
		$date = Zend_Date::now();
		$arr = array(
		    'ten_loai_thu'	=>$data['tendanhmucsua'],
		    'sap_xep'	=>$data['sapxep'],
			'ngay_sua'	    =>$date

		);		
		$where = "id =".$data['iddmsua'];
		$query = $this->_db->update($this->_dm,$arr,$where);
	return $query;
	}


	/**
	 * Quyen VV
	 * lay danh muc theo id
	 */
	public function getdanhmucthu($iddm)
	{
		$sql = $this->_db->select()
					->from($this->_dm)
					->where($this->_dm.'.id = ?',$iddm);
					$query = $this->_db->fetchRow($sql);
	return $query;
	}

}